package com.hqx.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hqx.common.utils.R;
import com.hqx.gulimall.product.dao.CategoryBrandRelationDao;
import com.hqx.gulimall.product.entity.CategoryBrandRelationEntity;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqx.common.utils.PageUtils;
import com.hqx.common.utils.Query;

import com.hqx.gulimall.product.dao.CategoryDao;
import com.hqx.gulimall.product.entity.CategoryEntity;
import com.hqx.gulimall.product.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Resource
    private CategoryBrandRelationDao categoryBrandRelationDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        List<CategoryEntity> categoryEntityList = baseMapper.selectList(null);
        return categoryEntityList.stream().filter(categoryEntity -> categoryEntity.getParentCid() == 0)
                .peek((menu) -> menu.setChildren(getChildren(menu,categoryEntityList)))
                .sorted(Comparator.comparingInt(CategoryEntity::getSort))
                .collect(Collectors.toList());
    }

    @Override
    public R removeMenuByIds(List<Long> asList) {

        //逻辑删除
       int result = baseMapper.deleteBatchIds(asList);
       return result == 0? R.error() : R.ok();
    }

    @Override
    public Long[] getCatelogIdsByCatelogId(Long catelogId) {
        List<Long> list = findparentCid(catelogId,new ArrayList<>());
        Collections.reverse(list);
        return list.toArray(new Long[list.size()]);
    }

    @Override
    @Transactional
    public boolean updateDetail(CategoryEntity category) {
        if(StringUtils.hasText(category.getName())){
            categoryBrandRelationDao.updateByCatelogId(category.getCatId(),category.getName());
        }
        return this.updateById(category);
    }

    private List<Long>  findparentCid(Long catelogId,List<Long> list) {
        list.add(catelogId);
        CategoryEntity categoryEntity = this.getById(catelogId);
        if(categoryEntity != null && categoryEntity.getParentCid() != 0) {
            findparentCid(categoryEntity.getParentCid(),list);
        }
        return list;
    }

    public List<CategoryEntity> getChildren(CategoryEntity root, List<CategoryEntity> all){
        return all.stream().filter(item -> item.getParentCid().equals(root.getCatId()))
                .peek((menu) -> menu.setChildren(getChildren(menu,all)))
                .sorted(Comparator.comparing(CategoryEntity::getSort,Comparator.nullsFirst(Comparator.naturalOrder())))// 空指针处理
                .collect(Collectors.toList());
    }

}